<script src="{{ getAssets }}js/amazeui.datatables.min.js?v={{ assets_version }}"></script>
<script>
var entry_id = '{{.entry_id}}';
var products_id = '{{.products_id}}';
var middle_id = '{{.middle_id}}';

var tables = $('#dataTables').DataTable({
    "sAjaxSource" : '{{ urlfor "EntryreturnController.SelectEntry"}}'
    ,"bLengthChange": false
    ,"fnServerData" : function(sSource, aoData, fnCallback) {
        var params = checkQueryTableData(aoData);
        params.products_id = Number(products_id);
        params.query_dr = $('#query_dr').val();
        params.query_am = $('#query_am').val();
        params.addlight = $('#addlight').val();
        params.model_number = $('#model_number').val();
        params.colour_number = $('#colour_number').val();
        params.isnegative = "1"; //去除负库存
        $.post(sSource, JSON.stringify(params),function(resp) {
            fnCallback(resp);
        },'json');
    }
    ,order:[]
    ,columns: [
        {"bSortable": false, render:function(data, type, full, meta) {
              return '<input type="checkbox" class="selectOne" value="'+ meta.row +'">';
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Products.Name;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.sph;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.cyl;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.add;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.model;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.color;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.size;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.function_info;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.film_color;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.genre;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Products.Spec;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Acceptance.lot_number;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Acceptance.manufacture_date > 0 ? timetransDate(full.Acceptance.manufacture_date) : '';
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Acceptance.expiry_date > 0 ? timetransDate(full.Acceptance.expiry_date) : '';
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return '<input type="hidden" id="o-purchase-'+ full.Id +'" value="'+ full.purchase_num +'">' + full.purchase_num;
        }}
    ]
    ,"aLengthMenu":[20, 50, 100, 200]
	,'bLengthChange': true
    ,"serverSide": true
    ,"bPaginate" : true
    ,"bFilter" : false	//是否启动过滤、搜索功能
    ,"bInfo" : true //是否显示页脚信息，DataTables插件左下角显示记录数
    ,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示
    ,"drawCallback": function(settings){
    }
});
var dataTables1 = $('#dataTables-1').DataTable({
    order:[]
    ,columns: [
        {"bSortable": false, render:function(data, type, full, meta) {
                return '<input type="checkbox" class="selectOne-1" data-id="'+ full.Id +'" value="'+ meta.row +'">';
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Products.Name;
        }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.sph;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.cyl;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.add;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.model;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.color;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.size;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.function_info;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.film_color;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Snd.genre;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Products.Spec;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Acceptance.lot_number;
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Acceptance.manufacture_date > 0 ? timetransDate(full.Acceptance.manufacture_date) : '';
            }}
        ,{"bSortable": false, render:function(data, type, full, meta) {
                return full.Acceptance.expiry_date > 0 ? timetransDate(full.Acceptance.expiry_date) : '';
        }}
        ,{"data" : "purchase_num","bSortable": false, render:function(data, type, full, meta) {
                var id = full.InventoryId == undefined ? full.Id : full.InventoryId;
                var prnum = $('#o-purchase-'+id).val();
                return '<input type="hidden" name="snd_id" class="invid" value="'+ full.Snd.id +'"><input type="hidden" name="invid" class="invid" value="'+ id +'"><input name="reserve_num" type="text" readonly value="'+ prnum +'">';
        }}
        ,{"data" : "purchase_num","bSortable": false, render:function(data, type, full, meta) {
                return '<input name="return_num" type="number" value="'+ data +'">';
        }}
    ]
    ,"serverSide": false
    ,"bPaginate" : false
    ,"bFilter" : false	//是否启动过滤、搜索功能
    ,"bInfo" : false //是否显示页脚信息，DataTables插件左下角显示记录数
    ,"bProcessing" : true //DataTables载入数据时，是否显示‘进度’提示
    ,"drawCallback": function(settings){
    }
});
$("#query-submit").bind("click", function () { //点击按钮 触发table重新请求服务器
    $("#dataTables").dataTable().fnDraw(false);
});
$('.selectAll').click(function(){
   if($(this).prop('checked') != false) {
       $('.selectOne').prop('checked', true);
   } else {
       $('.selectOne').prop('checked', false);
   }
});
$('.selectAll-1').click(function(){
    if($(this).prop('checked') != false) {
        $('.selectOne-1').prop('checked', true);
    } else {
        $('.selectOne-1').prop('checked', false);
    }
});
$('#dataTables').parent().slimscroll({
    height: '240px',
    width: '100%', //可滚动区域宽度
    size:"10px",
    color:'#0e90d2',
    railVisible: true, //是否 显示轨道
    alwaysVisible: true,
});
$('#dataTables-1').parent().slimscroll({
    height: '240px',
    width: '100%', //可滚动区域宽度
    size:"10px",
    color:'#0e90d2',
    railVisible: true, //是否 显示轨道
    alwaysVisible: true,
});
var _isset = [];
$('#batchInsertGoodsDetail').click(function(){
    var selLine = $('.selectOne[type="checkbox"]:checked');
    if(selLine.length <= 0) {
        layer.alert("请选择需要的行");return false;
    }
    var dt = $('#dataTables-1').DataTable();
    $.each(selLine, function(i, value){
        var datas = $('#dataTables').DataTable().row($(this).val()).data();
        if($.inArray(datas.Id, _isset) === -1) {
            dt.row.add(datas).draw();
            _isset.push(datas.Id);
        }
    });
});
$('#batchDeleteGoodsDetail').click(function(){
    var selLine = $('.selectOne-1[type="checkbox"]:checked');
    if(selLine.length <= 0) {
        layer.alert("请选择需要的行");return false;
    }
    $.each(selLine, function(index, value){
        var id = $(this).data('id');
        $('#dataTables-1').DataTable().row($(this).val()).remove().draw(false);
        for (var i = 0; i < _isset.length; i++) {
            if (_isset[i] == id){
                _isset.splice(i, 1);
            }
        }
    });
});

//获取中间表信息
function getMiddleData() {
    if(middle_id == '0') {
        return false;
    }
    var dt = $('#dataTables-1').DataTable();
    $.post('{{  urlfor "EntryreturnController.GetMiddleDataForEntry"}}', {'middle_id':Number(middle_id)}, function(res){
         if(res.code == 0) {
             $.each(res.data, function(index, value){
                 dt.row.add(value).draw();
                 _isset.push(value.InventoryId);
             });
         }
    }, 'json');

}
getMiddleData();

$('#submit-details').click(function(){
    var dt = $('.invid');
    if(dt.length <= 0) {
        layer.alert("请添加退货商品");return false;
    }
    var tr = $('#dataTables-1').find('tbody tr');
    var _data = [];
    var isErr = false;
    $.each(tr ,function(index, value){
        var _this = $(this);
        var invid = _this.find('[name="invid"]').val();
        var snd_id = _this.find('[name="snd_id"]').val();
        var reserve_num = _this.find('[name="reserve_num"]').val();
        var return_num = _this.find('[name="return_num"]').val();
        if(!return_num) {
            isErr = true;
            layer.alert("请填入退货数量", function(i){
                tr.eq(index).find('input[name="return_num"]').focus();
                layer.close(i);
            });return false;
        }
        if(return_num <= 0) {
            isErr = true;
            layer.alert("退货数量不能小于或的等于0", function (i) {
                tr.eq(index).find('input[name="return_num"]').focus();
                layer.close(i);
            });return false;
        }
        if(parseInt(return_num)>parseInt(reserve_num)){
            isErr = true;
            layer.alert("退货数量不能大于库存数量", function (i) {
                tr.eq(index).find('input[name="return_num"]').focus();
                layer.close(i);
            });return false;
        }
        _data.push({'goods_id':Number(products_id), 'middle_id':Number(middle_id),'invid':Number(invid), 'snd_id':Number(snd_id), 'reserve_num':Number(reserve_num),'return_num':Number(return_num),'entry_id':Number(entry_id)});
    });
    if(isErr) {
        return false;
    }
    $.post('{{ urlfor "EntryreturnController.SubmitOrderData"}}', JSON.stringify(_data), function (res) {
        if(res.code == 1) {
            layer.alert(res.msg);return false;
        }
        parent.closeSelectComplete();
    });
});

</script>